package cn.fl.project.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.condition.SysEmpBankAccQueryCondition;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.*;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.condition.PrjRbtbillApplyQueryCondition;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.service.IPrjRbtbillApplyService;
import cn.fl.project.service.IPrjRbtbillBscInfoService;
import cn.fl.project.vo.PrjRbtbillApplyVO;
import cn.fl.project.vo.PrjRbtbillBscInfoVO;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysDictionaryService;
import cn.fl.system.service.ISysEmpBankAccService;
import cn.fl.system.vo.SysEmpBankAccVO;
import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 返利开票申请表-针对支付表 控制层
 */
@Controller
@RequestMapping("/project/prjRbtbillApply")
public class PrjRbtbillApplyController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(PrjRbtbillApplyController.class);
	
	/**
	 * PrjRbtbillApply 服务层BEAN
	 */
	@Autowired
	private IPrjRbtbillApplyService prjRbtbillApplyService;
	@Autowired
	private IPrjRbtbillBscInfoService prjRbtbillBscInfoService;
	
	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryService sysDictionaryService;
	
	/**
	 * SysEmpBankAcc 服务层BEAN
	 */
	@Autowired
	private ISysEmpBankAccService sysEmpBankAccService;

    /**
     * 客户 服务层BEAN
     */
    @Autowired
    private ICstBscInfoService cstBscInfoService;//客户基础信息表

	
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toListPage", "加载PrjRbtbillApply列表页面.",CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("project/prjRbtbillApply/prjRbtbillApplyList");
		//开票状态
		List<KeyValueVO> audStsCdList = this.prjRbtbillApplyService.getAudStsCdList();
		view.addObject("audStsCdList", audStsCdList);
        // 事业部
        List<KeyValueVO> orgList = this.cstBscInfoService.searchBusinessOrgList();
        view.addObject("orgList", orgList);
		return view;
	}
	
	/**
	 * 到新增页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toAddPage", "加载PrjRbtbillApply Add页面.",CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("project/prjRbtbillApply/prjRbtbillApplyModify");
		view.addObject("rbtAmts","0.00");//返利金额
		view.addObject("billAmts","0.00");//开票金额
		view.addObject("billTpAmts","0.00");//税点金额
		view.addObject("rbtAmount","0");//返利金额

		String id = request.getParameter("applyId");
		if(StringUtils.isNotEmpty(id)){
			view.addObject("applyId",id);
			PrjRbtbillApplyVO applyVO = this.prjRbtbillApplyService.selectById(Long.valueOf(id));
			if(null != applyVO){
				view.addObject("accNo", applyVO.getAccNo());//收款账号
				if(StringUtils.isBlank(applyVO.getRcptUnt())){
					view.addObject("rcptUnt", CurrentThreadContext.getCurrentUserName());//用户姓名
				}else{
					view.addObject("rcptUnt", applyVO.getRcptUnt());//收款人姓名
				}
				view.addObject("bkNm", applyVO.getBkNm());//开户银行
				view.addObject("bkNo", applyVO.getBkNo());//开户行号
				view.addObject("rbtAmount",applyVO.getRbtAmt());//返利金额
				if(StringUtils.isNotEmpty(applyVO.getRbtAmt())){
					view.addObject("rbtAmts",DecimalUtils.convertFenToYuan(applyVO.getRbtAmt()));
				}
				if(StringUtils.isNotEmpty(applyVO.getBillAmt())){
					view.addObject("billAmts",DecimalUtils.convertFenToYuan(applyVO.getBillAmt()));
				}
				if(StringUtils.isNotEmpty(applyVO.getBillTpAmt())){
					view.addObject("billTpAmts",DecimalUtils.convertFenToYuan(applyVO.getBillTpAmt()));
				}
			}
		}else{
			//view.addObject("isNO", false);//有数据为true、否则false是没有数据
			SysEmpBankAccQueryCondition sysEmpBankAccQueryCondition =new SysEmpBankAccQueryCondition();
			sysEmpBankAccQueryCondition.setUsrId(CurrentThreadContext.getCurrentUserId());//取得当前用户userid
			List<SysEmpBankAccVO> selectEmpBankList = sysEmpBankAccService.selectEmpBankList(sysEmpBankAccQueryCondition);//去查询当前用户的银行卡数据
			if(CollectionUtils.isNotEmpty(selectEmpBankList)){
				SysEmpBankAccVO accVO = selectEmpBankList.get(selectEmpBankList.size()-1);
				//view.addObject("bkid", accVO.getId());//收款账号
				view.addObject("accNo", accVO.getAccNo());//收款账号
				if(StringUtils.isBlank(accVO.getRcptUnt())){
					view.addObject("rcptUnt", CurrentThreadContext.getCurrentUserName());//用户姓名
				}else{
					view.addObject("rcptUnt", accVO.getRcptUnt());//收款人姓名
				}
				view.addObject("bkNm", accVO.getBkNm());//开户银行
				view.addObject("bkNo", accVO.getBkNo());//开户行号
				//view.addObject("isNO", true);//有数据为true、
			}else{
				view.addObject("rcptUnt", CurrentThreadContext.getCurrentUserName());//用户姓名
			}
		}

		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toModifyPage", "加载PrjRbtbillApply Modify页面.",CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("project/prjRbtbillApply/prjRbtbillApplyModify");
		//this.prepare(request,mav);
		view.addObject("rcptUnt", CurrentThreadContext.getCurrentUserName());//用户姓名
		view.addObject("isNO", false);//有数据为true、否则false是没有数据
		SysEmpBankAccQueryCondition sysEmpBankAccQueryCondition =new SysEmpBankAccQueryCondition();
		sysEmpBankAccQueryCondition.setUsrId(CurrentThreadContext.getCurrentUserId());//取得当前用户userid
		List<SysEmpBankAccVO> selectEmpBankList = sysEmpBankAccService.selectEmpBankList(sysEmpBankAccQueryCondition);//去查询当前用户的银行卡数据
		if(CollectionUtils.isNotEmpty(selectEmpBankList)){
			for (int i = 0; i < selectEmpBankList.size(); i++) {
				view.addObject("bkid", selectEmpBankList.get(i).getId());//收款账号
				view.addObject("accNo", selectEmpBankList.get(i).getAccNo());//收款账号
				view.addObject("rcptUnt", selectEmpBankList.get(i).getRcptUnt());//收款人姓名
				view.addObject("bkNm", selectEmpBankList.get(i).getBkNm());//开户银行
				view.addObject("bkNo", selectEmpBankList.get(i).getBkNo());//开户行号
				view.addObject("isNO", true);//有数据为true、
			}
		}
	  /*List<String> codeGrpCdList = new ArrayList<>();
		codeGrpCdList.add(DictionaryConstant.CODE_GRP_CERT_TYP_CD);//证件类型
		codeGrpCdList.add(DictionaryConstant.CODE_GRP_CST_NP_BK_CD);//开户银行
		codeGrpCdList.add(DictionaryConstant.CODE_GRP_CST_NP_ACC_TYP_CD);//客户自然人 账户类型代码
		Map<String, List<KeyValueVO>> map = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
		//开户银行
		List<KeyValueVO> bkCdList =  map.get(DictionaryConstant.CODE_GRP_CST_NP_BK_CD);
		//证件类型
		List<KeyValueVO> certTypCdList =  map.get(DictionaryConstant.CODE_GRP_CERT_TYP_CD);
		//客户自然人 账户类型代码
		List<KeyValueVO> npAccTypCdList =  map.get(DictionaryConstant.CODE_GRP_CST_NP_ACC_TYP_CD);
		view.addObject("selectBkCdList", bkCdList);
		view.addObject("certTypCdList", certTypCdList);
		view.addObject("npAccTypCdList", npAccTypCdList);*/
		
		String id=request.getParameter("id");
		PrjRbtbillApplyVO prjRbtbillApplyVO = this.prjRbtbillApplyService.selectById(Long.valueOf(id));
		view.addObject("prjRbtbillApplyVO", prjRbtbillApplyVO);
		return view;
	}
	
	//加载数据字典数据、传值等 ？
    public void prepare(HttpServletRequest request,ModelAndView mav){
    	
    }

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initPrjRbtbillApplyList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		PrjRbtbillApplyQueryCondition qc = new PrjRbtbillApplyQueryCondition();
		// 申请时间
		String applyTm = request.getParameter("applyTm");
		// 申请人姓名
		String applyMgrNm = request.getParameter("applyMgrNm");
		// 报销状态
		String audStsCd = request.getParameter("audStsCd");
		// 事业部
		String buOrgCd = request.getParameter("buOrgCd");
		if (StringUtils.isNotEmpty(applyTm)) {
			qc.setApplyTm(DateUtils.formatStringToDate(applyTm, DateUtils.YYYY_MM_DD));
		}
		if (StringUtils.isNotEmpty(applyMgrNm)) {
			qc.setApplyMgrNm(applyMgrNm);
		}
		if (StringUtils.isNotBlank(audStsCd)) {
			qc.setAudStsCd(audStsCd);
		}
        if (StringUtils.isNotBlank(buOrgCd)) {
            qc.setBuOrgCd(buOrgCd);
        }
		page.setCondition(qc);
		Pager pager = this.prjRbtbillApplyService.searchListPage(page);
		List<PrjRbtbillApplyVO> list = (List<PrjRbtbillApplyVO>) pager.getResultList();
		ListPageVO<PrjRbtbillApplyVO> listPage = new ListPageVO<PrjRbtbillApplyVO>(list, pager.getTotalCount());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeByIds.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removePrjRbtbillApplyByIds(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String ids = request.getParameter("ids");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
        if (StringUtils.isEmpty(ids)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "removePrjRbtbillApplyByIds", "删除PrjRbtbillApply数据,主键参数为空!", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        List<Long> idsList = new ArrayList<Long>();
        JSONArray idsArray = JSONArray.fromObject(ids);
        for (Object object : idsArray) {
            idsList.add(Long.valueOf(object.toString()));
        }
		
		try {
			boolean success = this.prjRbtbillApplyService.removeByIds(idsList);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
				BaseLogUtils.info(logger, "removePrjRbtbillApplyByIds", "删除PrjRbtbillApply数据失败,ID:"+idsList.toString(), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "removePrjRbtbillApplyByIds", "删除PrjRbtbillApply数据成功,ID:"+idsList.toString(), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.info(logger, "removePrjRbtbillApplyByIds", "删除PrjRbtbillApply数据失败,ID:"+idsList.toString()+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removePrjRbtbillApplyByIds", "删除PrjRbtbillApply数据失败,系统错误.ID:"+idsList.toString()+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getPrjRbtbillApplyById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "getPrjRbtbillApplyById", "获取PrjRbtbillApply数据,主键参数为空.", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			PrjRbtbillApplyVO vo = this.prjRbtbillApplyService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getPrjRbtbillApplyById", "获取PrjRbtbillApply数据不存在,ID:"+id, CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getPrjRbtbillApplyById", "获取PrjRbtbillApply数据成功,ID:"+id, CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.info(logger, "getPrjRbtbillApplyById", "获取PrjRbtbillApply数据失败,ID:"+id+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getPrjRbtbillApplyById", "获取PrjRbtbillApply数据失败,系统错误.ID:"+id+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}


	/**
	 * 保存员工银行账户信息表 
	 */
	@RequestMapping(value = "/saveBKinfo.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object saveBKinfoApply(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		//获取参数
		String rcptUnt = request.getParameter("rcptUnt");
		String applyId = request.getParameter("applyId");
		String actualPaymentTm = request.getParameter("actualPaymentTm");//实际付款时间
		String actualPaymentAmt = request.getParameter("actualPaymentAmt");//实际付款金额
		String bkNm = request.getParameter("bkNm");
		String accNo = request.getParameter("accNo");
		String bkNo = request.getParameter("bkNo");
		try{
			if(StringUtils.isEmpty(applyId)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("申请ID不能为空");
				BaseLogUtils.info(logger, "saveBKinfoApply", "数据处理失败，申请ID不能为空.DATA:", CurrentThreadContext.getCurrentUserName());
				return result;

			}
			if(StringUtils.isEmpty(bkNo)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("开户行行号 不能为空！");
				BaseLogUtils.info(logger, "saveBKinfoApply", "保存员工银行账户信息表数据处理失败.DATA:", CurrentThreadContext.getCurrentUserName());
				return result;

			}
			if(StringUtils.isEmpty(bkNm)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("开户行及支行 不能为空！");
				BaseLogUtils.info(logger, "saveBKinfoApply", "保存员工银行账户信息表数据处理失败.DATA:", CurrentThreadContext.getCurrentUserName());
				return result;

			}
			if(StringUtils.isEmpty(accNo)){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("开户行账号 不能为空！");
				BaseLogUtils.info(logger, "saveBKinfoApply", "保存员工银行账户信息表数据处理失败.DATA:", CurrentThreadContext.getCurrentUserName());
				return result;
			}

			PrjRbtbillApplyVO vo = new PrjRbtbillApplyVO();
			vo.setId(Long.valueOf(applyId));
			vo.setRcptUnt(rcptUnt);
			vo.setBkNm(bkNm);
			vo.setAccNo(accNo);
			vo.setBkNo(bkNo);
			if (StringUtils.isNotBlank(actualPaymentAmt)) {
				vo.setActualPaymentAmt(DecimalUtils.convertYuanToFen(Double.parseDouble(actualPaymentAmt.replace(",",""))));
			}
			if (StringUtils.isNotBlank(actualPaymentTm)) {
				vo.setActualPaymentTm(DateUtils.formatStringToDate(actualPaymentTm,DateUtils.YYYY_MM_DD));
			}
			this.prjRbtbillApplyService.modify(vo);
			BaseLogUtils.info(logger, "saveBKinfoApply", "保存员工银行账户信息表数据处理完成.DATA:"+applyId, CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR+ex.getMessage());
			BaseLogUtils.info(logger, "saveBKinfoApply", "saveBKinfoApply保存员工银行账户信息表数据处理异常.DATA:"+applyId, CurrentThreadContext.getCurrentUserName(),ex);
		}
		return result;
	}
	
	
	
	
	
	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addPrjRbtbillApply(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		JSONObject param = new JSONObject();
		PrjRbtbillApplyVO entity = null;
		List<String> paySchNos = null;
		try {
			param = this.getRequestParamForPrjRbtbillApply(request);//获取参数
            entity = (PrjRbtbillApplyVO) param.get("entity");
            if (null != entity && PrjBscInfoConstant.CODE_BU_ORG_CD_ZK.equals(entity.getBuOrgCd())) {
                paySchNos = (List<String>) param.get("paySchNos");
                boolean isCheckOk = this.prjRbtbillApplyService.getStepRebateStateByPaySchNos(paySchNos);
                if (!isCheckOk) {
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("有效期内支付表阶梯返利申请全部完毕后方可发起返利报销流程");
                    return result;
                }
            }
            Long id = this.prjRbtbillApplyService.add(entity);
			//保存返利票据基础信息
			if(StringUtils.isNotEmpty(id)){
				@SuppressWarnings("unchecked")
				List<PrjRbtbillBscInfoVO> voList = (List<PrjRbtbillBscInfoVO>) param.get("voList");
				for (PrjRbtbillBscInfoVO vo : voList) {
					vo.setApplyId(id);
					prjRbtbillBscInfoService.add(vo);
				}
			}
			result = this.prjRbtbillApplyService.addFlowStart(id, result);
			result.setData(id);
			BaseLogUtils.info(logger, "addPrjRbtbillApply", "新增PrjRbtbillApply数据成功,ID:"+id+".DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR+ex.getMessage());
			BaseLogUtils.info(logger, "addPrjRbtbillApply", "新增PrjRbtbillApply数据失败.DATA:"+JSONObject.toJSONString(entity)+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR+ex.getMessage());
			BaseLogUtils.error(logger, "addPrjRbtbillApply", "新增PrjRbtbillApply数据失败,系统错误.DATA:"+JSONObject.toJSONString(entity)+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 修改
	 */
	@SuppressWarnings("null")
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modifyPrjRbtbillApply(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,SysLogConstant.LOG_CMM_UPDATE_SUCC);
		PrjRbtbillApplyVO entity = null;
		try {
			entity = null;
			JSONObject param = this.getRequestParamForPrjRbtbillApply(request);//获取参数
			entity = (PrjRbtbillApplyVO) param.get("entity");
			if(null == entity){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改返利报销信息失败");
				BaseLogUtils.info(logger, "modifyPrjRbtbillApply", "修改返利报销信息失败,请求参数为空！.", CurrentThreadContext.getCurrentUserName());
				return result;
			}
			if(StringUtils.isEmpty(entity.getId())){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
				BaseLogUtils.info(logger, "modifyPrjRbtbillApply", "修改返利报销信息,主键参数为空！.", CurrentThreadContext.getCurrentUserName());
				return result;
			}
			//修改报销申请信息
			boolean success = this.prjRbtbillApplyService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_UPDATE_FAIL);
				BaseLogUtils.info(logger, "modifyPrjRbtbillApply", "修改PrjRbtbillApply数据失败.DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
				return result;
			}
			//根据申请ID删除报销明细
			boolean flag = this.prjRbtbillBscInfoService.delByApplyId(entity.getId());
			if(!flag){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改报销明细失败");
				return result;
			}
			List<PrjRbtbillBscInfoVO> voList = (List<PrjRbtbillBscInfoVO>) param.get("voList");
            if (CollectionUtils.isNotEmpty(voList)) {
                for (PrjRbtbillBscInfoVO vo : voList) {
                    vo.setApplyId(entity.getId());
                    prjRbtbillBscInfoService.add(vo);
                }
            }
			BaseLogUtils.info(logger, "modifyPrjRbtbillApply", "修改PrjRbtbillApply数据成功.DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR+ex.getMessage());
			BaseLogUtils.info(logger, "modifyPrjRbtbillApply", "修改PrjRbtbillApply数据失败.DATA:"+JSONObject.toJSONString(entity)+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR+ex.getMessage());
			BaseLogUtils.error(logger, "modifyPrjRbtbillApply", "修改PrjRbtbillApply数据失败,系统错误.DATA:"+JSONObject.toJSONString(entity)+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 获取请求参数
	 * @param request 请求
	 * @return 
	 */
	private JSONObject getRequestParamForPrjRbtbillApply(HttpServletRequest request) throws BaseException{
		JSONObject param = new JSONObject();
		//获取参数
		String id = request.getParameter("id");//主键
		String billAmt = request.getParameter("billAmt");//开票金额(分)
		String rbtAmt = request.getParameter("rbtAmt");//返利金额(分)
		String billTpPct = request.getParameter("billTpPct");//开票税点
		String billTpAmt = request.getParameter("billTpAmt");//开票税点金额
		Long applyMgrId = CurrentThreadContext.getCurrentUserId();
		String applyMgrNm = CurrentThreadContext.getCurrentUserName();//申请人名称
		String applyMgrOrgCd = CurrentThreadContext.getCurrentOrgCd();//申请人组织架构代码
		String buOrgCd = request.getParameter("buOrgCd");//所属事业部
		String actualPaymentTm = request.getParameter("actualPaymentTm");//实际付款时间
		String actualPaymentAmt = request.getParameter("actualPaymentAmt");//实际付款金额
		String rcptUnt = request.getParameter("rcptUnt");//收款单位
		String bkNm = request.getParameter("bkNm");
		String accNo = request.getParameter("accNo");
		String bkNo = request.getParameter("bkNo");
		if(StringUtils.isEmpty(bkNo)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "开户行行号 不能为空.");
		}
		if(StringUtils.isEmpty(bkNm)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "开户行及支行不能为空！");

		}
		if(StringUtils.isEmpty(accNo)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "开户行账号不能为空！");
		}

		//验证参数并赋值
		PrjRbtbillApplyVO entity = new PrjRbtbillApplyVO();
 		entity.setBillAmt(DecimalUtils.convertYuanToFen(Double.valueOf(billAmt)));//开票金额(分)
 		entity.setRbtAmt(DecimalUtils.convertYuanToFen(Double.valueOf(rbtAmt)));//返利金额(分)
 		entity.setBillTpPct(Double.valueOf(billTpPct));//开票税点
 		entity.setBillTpAmt(DecimalUtils.convertYuanToFen(Double.valueOf(billTpAmt)));//开票税点金额
 		entity.setApplyMgrId(applyMgrId);//申请人ID
 		entity.setApplyMgrNm(applyMgrNm);//申请人名称
 		entity.setApplyMgrOrgCd(applyMgrOrgCd);//申请人组织架构代码
 		entity.setApplyTm(new Date());//申请时间
		entity.setBuOrgCd(buOrgCd);
		entity.setRcptUnt(rcptUnt);
		entity.setBkNm(bkNm);
		entity.setBkNo(bkNo);
		entity.setAccNo(accNo);

		if(StringUtils.isNotBlank(actualPaymentTm)){
			entity.setActualPaymentTm(DateUtils.formatStringToDate(actualPaymentTm,DateUtils.YYYY_MM_DD));
		}
		if(StringUtils.isNotBlank(actualPaymentAmt)){
			if (!StringUtils.isUnsignedNumeric(actualPaymentAmt)) {
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "请填写正确的实付金额.");
			}else{
				entity.setActualPaymentAmt(DecimalUtils.convertYuanToFen(Double.parseDouble(actualPaymentAmt)));
			}
		}


		Date tm=new Date();
		if (StringUtils.isNotBlank(id)) {
			entity.setId(Long.valueOf(id));
			entity.setMdfTm(tm);
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
		}else{
			entity.setCrtTm(tm);
			entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			//entity.setCrtUsrOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前用户组织机构代码
            //entity.setOrgCd(CurrentThreadContext.getCurrentBuOrgCd());//事业部组织机构代码
		}
		//获取返利票据基础信息
		com.alibaba.fastjson.JSONArray applyList = JSONObject.parseArray(request.getParameter("applyListJson"));
		List<PrjRbtbillBscInfoVO> voList = new ArrayList<PrjRbtbillBscInfoVO>();
        List<String> paySchNos = new ArrayList<>(applyList.size());
        if(CollectionUtils.isNotEmpty(applyList)){
			for (int i = 0; i < applyList.size(); i++) {
				PrjRbtbillBscInfoVO vo = new PrjRbtbillBscInfoVO();
				JSONObject jo = (JSONObject) applyList.get(i);
				if(StringUtils.isBlank(entity.getBuOrgCd())&&StringUtils.isNotBlank(jo.getString("buOrgCd"))){
					//设置事业部
					entity.setBuOrgCd(jo.getString("buOrgCd"));
				}
		 		vo.setPaySchNo(jo.getString("paySchNo"));//支付表编号
                paySchNos.add(jo.getString("paySchNo"));
		 		vo.setPaySchId(Long.valueOf(jo.getString("paySchId")));//支付表ID
		 		vo.setSplRbtAmt(Long.parseLong(jo.getString("splRbtAmt")));//经销商返利金额(分)
		 		vo.setGpsRbtAmt(Long.parseLong(jo.getString("gpsRbtAmt")));//GPS返利金额(分)
                vo.setSaleRbtAmt(Long.parseLong(jo.getString("saleRbtAmt")));//销售返利金额(分)
				if(StringUtils.isNotBlank(jo.getString("lsDt"))){
					vo.setLsDt(DateUtils.formatDate(new Date(Long.valueOf(jo.getString("lsDt"))),DateUtils.YYYY_MM_DD));//起租日
				}
		 		vo.setCrtUsrId(applyMgrId);
		 		vo.setCrtTm(tm);
		 		voList.add(vo);
			}
		}else{
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "请选择要报销的支付表.");
		}
		if(StringUtils.isBlank(entity.getBuOrgCd())){
			entity.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());//所属事业部
		}
		param.put("entity", entity);
		param.put("voList", voList);
		param.put("paySchNos", paySchNos);
		return param;
	}
	
}

